<?php
// +----------------------------------------------------------------------
// | Author: wyd
// +----------------------------------------------------------------------
// | Date: 2025/7/15
// +----------------------------------------------------------------------

declare(strict_types=1);


namespace Study\annotations;


use Doctrine\Common\Annotations\AnnotationReader;
use http\Exception\RuntimeException;

class AnnotationHelper
{
    private $reader;

    public function __construct(AnnotationReader $reader)
    {
        $this->reader = $reader;
    }

    public function getClassAnnotations(string $className): array
    {
        try {
            return $this->reader->getClassAnnotations(
                new \ReflectionClass($className)
            );
        } catch (\ReflectionException $e) {
             throw new RuntimeException($e->getMessage());
        }
    }

    public function getPropertyAnnotations(string $className, string $propertyName): array
    {
        try {
            return $this->reader->getPropertyAnnotations(
                new \ReflectionProperty($className, $propertyName)
            );
        } catch (\ReflectionException $e) {
            throw new RuntimeException($e->getMessage());
        }
    }

    public function getMethodAnnotations(string $className, string $methodName): array
    {
        try {
            return $this->reader->getMethodAnnotations(
                new \ReflectionMethod($className, $methodName)
            );
        } catch (\ReflectionException $e) {
            throw new RuntimeException($e->getMessage());
        }
    }

    // 获取特定类型的注解
    public function getClassAnnotation(string $className, string $annotationClass): ?object
    {
        try {
            return $this->reader->getClassAnnotation(new \ReflectionClass($className), $annotationClass);
        } catch (\ReflectionException $e) {
            throw new RuntimeException($e->getMessage());
        }
    }
}